/***************************************************

This plugin is to test if players have set 32-bit.

Multiple pictures are show to each player and he or she needs to
choose proper one (in menu). If player does not choose properly
after X tries he or she is disconnected. Pictures are show after
the player dies, not to disturb the gameplay.

CVARs (default):
32bit_max_tries "2"
32bit_immunity_flag ""

Plugin is using "data/lang/32bit_test.txt" language file.

***************************************************/

#pragma semicolon 1
#include <amxmodx> 
#include <amxmisc>
#define TASKID 18923

new g_test_details[33][3];
new g_tested[33];
new g_tries[33];
new g_quick_test[33];
new g_immunity_flag;
new p_tested32bit[33];
new p_max_tries;
new p_immunity_flag;
new p_warmup_active; //this cvar is to prevent from testing players during warmup. When "amx_warmup_active" is "1" then tests do not occur (you can use it for custom warmup)

public plugin_init(){
	register_plugin("32-bit test","2.0","Czesio");
	register_dictionary("32bit_test.txt");
	register_event("DeathMsg", "event_death_msg", "a");
	
	p_max_tries = register_cvar("32bit_max_tries", "2");
	p_immunity_flag = register_cvar("32bit_immunity_flag", "");
	p_warmup_active = register_cvar("amx_warmup_active", "0");
	
	new tmp[32];
	for(new i=1; i<33; i++){
		formatex(tmp,31,"tested32bit_%d",i);
		p_tested32bit[i] = register_cvar(tmp,"0");
		g_tested[i] = get_pcvar_num(p_tested32bit[i]);
	}
	
	get_pcvar_string(p_immunity_flag, tmp, 31);
	g_immunity_flag = read_flags(tmp);
	
	register_menucmd(register_menuid("menu_start"), 511, "menu_start_handler");
	register_menucmd(register_menuid("menu_a"), 511, "menu_a_handler");
	register_menucmd(register_menuid("menu_b"), 511, "menu_b_handler");
	register_menucmd(register_menuid("menu_c"), 511, "menu_c_handler");
	
	return PLUGIN_CONTINUE;
}

public client_connect(id){
	g_tries[id] = 0;
	g_quick_test[id] = 0;
}

public event_death_msg(){
	new victim = read_data(2);
	new info[4];
	get_user_info(victim, "test_skip", info, 3);
	remove_quotes(info);
	if(equali(info, "Y")){
		g_tested[victim] = get_user_userid(victim);
		new name[32];
		get_user_name(victim, name, 31);
		log_amx("[TEST] Skipping player %s", name);
		return PLUGIN_HANDLED;
	} else {	
		set_task(1.5,"test_player", victim + TASKID);
	}
	return PLUGIN_HANDLED;
}

public test_player(id){
	id -= TASKID;
	
	if(get_pcvar_num(p_warmup_active))
		return;
	
	if(is_user_connected(id) && !is_user_bot(id) && !is_user_hltv(id) && g_tested[id] != get_user_userid(id)){
		if(get_user_flags(id) & g_immunity_flag){
			g_tested[id] = get_user_userid(id);
			return;
		}
		
		g_tries[id]++;
		
		if(g_tries[id] > get_pcvar_num(p_max_tries)){
			test_failed(id);
		}
		else{
			g_test_details[id][0] = random(8) + 1;
			g_test_details[id][1] = random(8) + 1;
			g_test_details[id][2] = random(8) + 1;
			if(g_quick_test[id])
				show_menu_test(id,0);
			else
				show_menu_start(id);
		}
	}
}

public menu_start_handler(id,key){
	key++;
	if(key == 1)
		show_menu_test(id,0);
	else if(key == 2)
		kick_player(id);
	else if(key == 4)
		show_more_info(id);
	else if(key == 5){
		g_quick_test[id] = 1;
		show_menu_test(id,0);
	}
	else
		show_menu_start(id);
}

public menu_a_handler(id,key){
	menu_test_handler(id, key, 0);
}
public menu_b_handler(id,key){
	menu_test_handler(id, key, 1);
}
public menu_c_handler(id,key){
	menu_test_handler(id, key, 2);
}

public menu_test_handler(id, key, stage){
	key++;
	if(key == 9)
		show_menu_test(id, stage);
	else if(stage == 2 && key == g_test_details[id][stage])
		test_passed(id);
	else if(key == g_test_details[id][stage])
		show_menu_test(id, stage + 1);
	else
		test_failed(id);
}

public show_menu_start(id){
	new menu_body[1000];
	formatex(menu_body, 999, "\w%L^n^n\w1. %L^n\w2. %L^n^n\w4. %L^n\w5. %L", id, "WELCOME", id, "WELCOME_ANS1", id, "WELCOME_ANS2", id, "WELCOME_ANS4", id, "WELCOME_ANS5");
	show_menu(id, 511, menu_body, -1, "menu_start");
}

public show_menu_test(id, stage){
	new menu_body[1000];
	
	if(stage == 0){
		formatex(menu_body, 999, "\w%L^n^n\w1. A1^n\w2. A2^n\w3.\w A3^n\w4.\w A4^n\w5.\w A5^n\w6.\w A6^n\w7.\w A7^n\w8.\w A8^n^n\w9. %L", id, "CHOOSE_PICTURE", id, "SHOW_AGAIN");
		show_menu(id, 511, menu_body, -1, "menu_a");
	}
	else if(stage == 1){
		formatex(menu_body, 999, "\w%L^n^n\w1. B1^n\w2. B2^n\w3.\w B3^n\w4.\w B4^n\w5.\w B5^n\w6.\w B6^n\w7.\w B7^n\w8.\w B8^n^n\w9. %L", id, "CHOOSE_PICTURE", id, "SHOW_AGAIN");
		show_menu(id, 511, menu_body, -1, "menu_b");
	}
	else{
		formatex(menu_body, 999, "\w%L^n^n\w1. C1^n\w2. C2^n\w3.\w C3^n\w4.\w C4^n\w5.\w C5^n\w6.\w C6^n\w7.\w C7^n\w8.\w C8^n^n\w9. %L", id, "CHOOSE_PICTURE", id, "SHOW_AGAIN");
		show_menu(id, 511, menu_body, -1, "menu_c");
	}
	if(!g_quick_test[id] || stage == 0)
		do_motd(id, stage);
}

public show_more_info(id){
	do_motd(id, -1);
	show_menu_start(id);
}

public do_motd(id,stage){
	new motd[2000];
	new find_string[50];
	new replace_string[50];
	
	motd = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd'><html><head><style type='text/css'>body{background:black;color:#fff;font:bold 16px Arial;text-align:center;margin:0;padding:0}th,td{padding-top:4px}b,u{display:inline-block;width:16px;height:48px}td b{background:#808080}td u{background:#7f7f7f}th b{background:#878787}th u{background:#787878}</style><meta http-equiv='content-type' content='text/html;charset=UTF-8'/></head>";
	
	if(stage != -1 && g_quick_test[id] == 0){
		new c;
		if(stage == 0)
			c = 'A';
		else if(stage == 1)
			c = 'B';
		else if(stage == 2)
			c = 'C';
		
		format(motd,1999,"%s<body><br/>%L<br/><br/><table cellpadding='0' cellspacing='5' style='margin:0 auto'><tr><td>%c1<br><b></b><u></u><b></b></td><td>%c2<br><b></b><u></u><b></b></td><td>%c3<br><b></b><u></u><b></b></td><td>%c4<br><b></b><u></u><b></b></td><td>%c5<br><b></b><u></u><b></b></td><td>%c6<br><b></b><u></u><b></b></td><td>%c7<br><b></b><u></u><b></b></td><td>%c8<br><b></b><u></u><b></b></td></tr></table><br/>%L %d / 3</body></html>",motd,id,"DETAILS",c,c,c,c,c,c,c,c,id,"STAGE",stage+1);
		
		formatex(find_string,49,"<td>%c%d<br><b></b><u></u><b></b></td>",c, g_test_details[id][stage]);
		formatex(replace_string,49,"<th>%c%d<br><b></b><u></u><b></b></th>",c, g_test_details[id][stage]);
		replace(motd, 1999, find_string, replace_string);
	}
	else if(stage != -1){
		strcat(motd,"<body><table cellpadding='0' cellspacing='5' style='margin:0 auto'><tr><td>A1<br><b></b><u></u><b></b></td><td>A2<br><b></b><u></u><b></b></td><td>A3<br><b></b><u></u><b></b></td><td>A4<br><b></b><u></u><b></b></td><td>A5<br><b></b><u></u><b></b></td><td>A6<br><b></b><u></u><b></b></td><td>A7<br><b></b><u></u><b></b></td><td>A8<br><b></b><u></u><b></b></td></tr>",1999);
		strcat(motd,"<tr><td>B1<br><b></b><u></u><b></b></td><td>B2<br><b></b><u></u><b></b></td><td>B3<br><b></b><u></u><b></b></td><td>B4<br><b></b><u></u><b></b></td><td>B5<br><b></b><u></u><b></b></td><td>B6<br><b></b><u></u><b></b></td><td>B7<br><b></b><u></u><b></b></td><td>B8<br><b></b><u></u><b></b></td></tr>",1999);
		strcat(motd,"<tr><td>C1<br><b></b><u></u><b></b></td><td>C2<br><b></b><u></u><b></b></td><td>C3<br><b></b><u></u><b></b></td><td>C4<br><b></b><u></u><b></b></td><td>C5<br><b></b><u></u><b></b></td><td>C6<br><b></b><u></u><b></b></td><td>C7<br><b></b><u></u><b></b></td><td>C8<br><b></b><u></u><b></b></td></tr></table></body></html>",1999);
	
		formatex(find_string,49,"<td>A%d<br><b></b><u></u><b></b></td>",g_test_details[id][0]);
		formatex(replace_string,49,"<th>A%d<br><b></b><u></u><b></b></th>",g_test_details[id][0]);
		replace(motd,1999,find_string,replace_string);
		
		formatex(find_string,49,"<td>B%d<br><b></b><u></u><b></b></td>",g_test_details[id][1]);
		formatex(replace_string,49,"<th>B%d<br><b></b><u></u><b></b></th>",g_test_details[id][1]);
		replace(motd,1999,find_string,replace_string);
		
		formatex(find_string,49,"<td>C%d<br><b></b><u></u><b></b></td>",g_test_details[id][2]);
		formatex(replace_string,49,"<th>C%d<br><b></b><u></u><b></b></th>",g_test_details[id][2]);
		replace(motd,1999,find_string,replace_string);
	}
	else{
		format(motd,1999,"%s<body><br/>%L</body></html>", motd, id, "MORE_INFO");
	}
	
	show_motd(id, motd, "32-bit test");
}

public test_passed(id){
	g_tested[id] = get_user_userid(id);
	set_pcvar_num(p_tested32bit[id], g_tested[id]);
	client_print(id,print_chat,"[TEST] %L",LANG_PLAYER,"TEST_OK");
}

public test_failed(id){
	if(g_tries[id] >= get_pcvar_num(p_max_tries)){
		kick_player(id);
	}
	else{
		if(get_pcvar_num(p_max_tries) - g_tries[id] == 1)
			client_print(id,print_chat,"[TEST] %L",LANG_PLAYER,"TEST_BAD_1_TRY_LEFT",get_pcvar_num(p_max_tries) - g_tries[id]);
		else
			client_print(id,print_chat,"[TEST] %L",LANG_PLAYER,"TEST_BAD",get_pcvar_num(p_max_tries) - g_tries[id]);
	}
}

public kick_player(id){
	server_cmd("kick #%d 16-bit video", get_user_userid(id));
}
